package com.nbers.springcloud.microservice.controller;

import com.nbers.springcloud.microservice.controller.base.BaseController;
import com.nbers.springcloud.microservice.entity.base.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;

/**
 * 登录服务接口
 * @author SIMON
 */
@RestController
@RequestMapping(value = "/login")
@Api("登录服务接口")
@RefreshScope//动态更新配置
public class LoginController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /***
     * start
     * 动态获取spring.datasource.druid.url的demo
     */
    @Value("${nbers.author}")
    private String author;

    @Value("${nbers.email}")
    private String email;

    @ApiOperation(value = "动态获取作者信息")
    @GetMapping(value = "/getAuthor")
    public JsonResult<String> getAuthor(){
        return new JsonResult<String>(JsonResult.SUCCESS,"更改git上的配置文件后，需POST执行http://127.0.0.1:8082/actuator/refresh触发更新",
                "From @Value "+author+"&"+email );
    }
    /***
     * end
     */


    @ApiOperation(value = "用户登录接口")
    @GetMapping(value = {"/","login"})
    public JsonResult<String> login() {
        JsonResult<String> jsonResult=new JsonResult<String>(JsonResult.SUCCESS);
        jsonResult.setData("未实现");
        return jsonResult;
    }

    /***
     * 打印Principle
     * @param oAuth2Authentication
     * @param principal
     * @param authentication
     * @return
     */
    @ApiOperation(value = "打印Principle接口")
    @GetMapping(value = "/getPrinciple")
    public OAuth2Authentication getPrinciple(OAuth2Authentication oAuth2Authentication, Principal principal, Authentication authentication) {
        logger.info(oAuth2Authentication.getUserAuthentication().getAuthorities().toString());
        logger.info(oAuth2Authentication.toString());
        logger.info("principal.toString() " + principal.toString());
        logger.info("principal.getName() " + principal.getName());
        logger.info("authentication: " + authentication.getAuthorities().toString());
        return oAuth2Authentication;
    }


}
